<template>
  <div class="app-container">
    <div style="text-align: center;margin-bottom: 30px">
      视频设备{{ createStatus ? '新增' : '更新' }}
    </div>
    <el-row :gutter="10">
      <el-form :model="cameraForm" ref="cameraForm" :rules="cameraFormRules" label-width="100px">
          <el-form-item label="视频设备名称" prop="name">
            <el-input type="text" v-model="cameraForm.name" placeholder="请输入视频设备名称"
                      clearable></el-input>
          </el-form-item>
          <el-form-item label="设备编号" prop="code">
            <el-input type="text" v-model="cameraForm.code" placeholder="请输入设备编号"
                      clearable></el-input>
          </el-form-item>
          <el-form-item label="设备IP地址" prop="ip">
            <el-input type="text" v-model="cameraForm.ip" placeholder="请输入设备IP地址"
                      clearable></el-input>
          </el-form-item>
          <el-form-item label="所属区域"  prop="regionId">
            <el-select v-model="cameraForm.regionId" filterable placeholder="请选择设备区域" style="width: 100%">
              <el-option v-for="(item,index) in regionList" :key="index" :value="item.ucId"
                         :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="设备类型"  prop="type">
            <el-select v-model="cameraForm.type" filterable placeholder="请选择设备类型" style="width: 100%">
              <el-option
                  v-for="dict in warningTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="给支队预警时间" prop="warningTime">
            <el-input type="text" v-model="cameraForm.warningTime" placeholder="请输入给支队预警时间"
                      clearable></el-input>
          </el-form-item>

      </el-form>
    </el-row>


    <div slot="footer" style="text-align: center" class="dialog-footer">
      <el-button @click="cancelData">取 消</el-button>
      <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createData">创 建
      </el-button>
      <el-button :loading="addLoading" v-else type="primary" @click="updateData">修 改</el-button>
    </div>
  </div>
</template>

<script>
import {getData, addData, updateData} from "@/api/camera";
import {getToken} from "@/utils/auth";
import {getInEnterpriseDataList} from "@/api/enterpriseDominant";
import {getRegionList} from "@/api/enterprise";
import {isNumber} from "@/utils/validate";

export default {
  name: "dataEdit",
  components: {},
  data() {
    return {
      cameraForm: {
        name: "",
        regionId: "",
        code: "",
        ip: ""
      },
      cameraFormRules: {
        name: [{required: true, message: '设备名称不能为空', trigger: 'blur'}],
        warningTime: [
            {validator: isNumber, trigger: "blur"}
        ]

      },
      addLoading: false,
      regionList: [],
      warningTypeOptions: []
    }
  },
  computed: {
    createStatus() {
      return this.$route.params.ucId == '0'
    },
    enterPriseHidden(){
      let roleCode = this.$store.getters.roleCode;
      return roleCode=="XT-ADMIN"
    },
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getRegionList()
    this.getDicts("warning_type").then(response => {
      this.warningTypeOptions = response.data.data;
    });
    if (!this.createStatus) {
      let ucId = this.$route.params.ucId
      this.getData(ucId)
    }
  },
  methods: {
    getRegionList(){
      getRegionList({}).then(res=>{
        this.regionList = res.data
      })
    },
    getData(val) {
      getData({ucId: val}).then(res => {
        this.cameraForm = res.data
      }).catch(res => {

      })
    },
    cancelData() {
      window.history.back()
    },
    createData() {
      this.addLoading = true
      this.$refs['cameraForm'].validate(valid => {
        if (valid) {
          console.log(this.cameraForm)
          addData(this.cameraForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "添加视频设备成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
    updateData() {
      this.addLoading = true
      this.$refs['cameraForm'].validate(valid => {
        if (valid) {
          updateData(this.cameraForm).then(res => {
            this.addLoading = false
            this.$notify.success({
              title: '成功',
              message: "更新视频设备成功"
            })
            this.cancelData()
          }).catch(res => {
            this.addLoading = false
            this.$notify.error({
              title: '失败',
              message: res.msg
            })
          })
        } else {
          this.addLoading = false
          console.log("表单验证不通过")
        }
      })
    },
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 149px;
  height: 149px;
  line-height: 149px;
  text-align: center;
}

.avatar {
  width: 149px;
  height: 149px;
  display: block;
}
</style>
